<template>
  <div class="app-container">
    <el-dialog title="选择学校" :visible.sync="localOpen" width="800px" :close-on-click-modal="true"
      @close="closeSelectSchool" append-to-body>
      <el-form :model="selectSchoolQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
        label-width="100px">
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="selectSchoolQueryParams.schoolName" placeholder="请输入学校名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="学校简称" prop="schoolAliaeName">
          <el-input v-model="selectSchoolQueryParams.schoolAliaeName" placeholder="请输入学校简称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="selectSchoolQueryParams.contacts" placeholder="请输入联系人" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="所属代理商" prop="agentName">
          <el-input v-model="selectSchoolQueryParams.agentName" placeholder="请输入所属代理商"
            @click.native="openSelectAgent(1)" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="selectSchoolQueryParams.tel" placeholder="请输入联系电话" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
          <el-cascader placeholder="请选择省、市、区" v-model="selectSchoolQueryParams.queryProvinceAndCityAndDistrict"
            :options="options" @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
            :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
            clearable></el-cascader>
        </el-form-item>
        <el-form-item label="上线" prop="state">
          <el-select v-model="selectSchoolQueryParams.state" placeholder="请选择" clearable>
            <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-table v-loading="loading" ref="selectSchoolTable" :data="schoolinfoList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" />
        <el-table-column label="id" align="center" prop="id" :show-overflow-tooltip="true" />
        <el-table-column label="联系人" align="center" prop="contacts" :show-overflow-tooltip="true" />
        <el-table-column label="学校全称" align="center" prop="schoolName" :show-overflow-tooltip="true" />
        <el-table-column label="学校简称" align="center" prop="schoolAliaeName" />
        <el-table-column label="招生人数" align="center" prop="studentCapacity" :show-overflow-tooltip="true" />
        <el-table-column label="代理商" align="center" prop="agentName" />
        <el-table-column label="联系电话" align="center" prop="tel" />
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="selectSchoolQueryParams.pageNum"
        :limit.sync="selectSchoolQueryParams.pageSize" @pagination="getList" />

      <!-- 选择代理 -->
      <SelectAgent ref="selectAgent" :open="selectFlag" @selected-data="selectAgentData"
        @close-select-window="closeSelectAgent" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="selectSelectData">确 定</el-button>
        <el-button @click="closeSelectSchool">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSchoolinfo,
  } from "@/api/bus_web/school/schoolinfo";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  import SelectAgent from "../../agent/agent/select.vue"
  import {
    mapState
  } from 'vuex';
  import store from "@/store";
  export default {
    name: "SelectSchoolinfo",
    dicts: ['school_state', 'school_type'],
    props: {
      open: Boolean
    },
    components: {
      SelectAgent
    },
    data() {
      return {
        options: [],
        localOpen: this.open,
        // 是否显示代理选择
        selectFlag: false,
        //选中代理
        selectedAgentData: [],
        selectSchoolTable: [],
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 学校管理表格数据
        schoolinfoList: [],
        // 查询参数
        selectSchoolQueryParams: {
          pageNum: 1,
          pageSize: 10,
          schoolName: null,
          schoolAliaeName: null,
          agentId: null,
          agentIdArray: [],
          agentName: null,
          schoolType: null,
          levelOfEducation: null,
          tel: null,
          introduce: null,
          province: null,
          city: null,
          district: null,
          address: null,
          state: null,
          queryProvinceAndCityAndDistrict: null,
        },
      };
    },
    created() {
      //  this.getList();
    },
    watch: {
      open(newValue, oldValue) {
        this.localOpen = newValue; // 同步父组件的 open 值到本地状态
      },
      localOpen(newValue) {
        if (newValue) {
          this.getList() // 当本地状态变为 false 时，通知父组件
        } else {
          this.closeSelectAgent()
        }
      }
    },
    computed: {
      ...mapState({
        agent: state => state.selectAgent.agent,
        agentSelectModel: state => state.selectAgent.agentSelectModel,
        school: state => state.selectSchool.school,
        schoolSelectModel: state => state.selectSchool.schoolSelectModel,
      })
    },
    methods: {
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      /** 查询学校管理列表 */
      getList() {
        this.loading = true;
        listSchoolinfo(this.selectSchoolQueryParams).then(response => {
          this.schoolinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.selectSchoolQueryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.selectSchoolQueryParams.pageNum = 1
        this.selectSchoolQueryParams.agentId = null
        this.selectSchoolQueryParams.agentIdArray = []
        this.handleQuery();

      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        if (selection.length > 1 && this.schoolSelectModel == 2) {
          selection = []
          this.$refs.selectSchoolTable.clearSelection()
          this.$store.dispatch('selectSchool/cleanSchoolData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectSchool/setSchool', item)
        })

      },
      //选择代理
      openSelectAgent(selectVal) {
        this.selectFlag = true
        // selectVal =1 查询模式 =2 创建、修改模式
        this.$store.dispatch('selectAgent/cleanAgent')
        this.$store.dispatch('selectAgent/setAgentSelectModel', selectVal)
      },
      closeSelectAgent() {
        this.selectFlag = false
      },


      //确定代理
      selectAgentData() {
        this.closeSelectAgent()
        if (this.agentSelectModel == 2) {
          this.form.agentName = this.agent[0].agentName
          this.form.agentId = this.agent[0].id
        } else {
          this.selectSchoolQueryParams.agentName = this.agent.map(item => item.agentName).join(',')
          this.selectSchoolQueryParams.agentIdArray = this.agent.map(item => item.id)
        }
        this.$store.dispatch('selectAgent/cleanAgent')
      },
      //确定选择学校
      selectSelectData() {
        if (this.school.length < 1) {
          this.$modal.msgError("请至少选中1条学校")
          return
        }
        this.resetForm("queryForm")
        this.selectSchoolQueryParams.agentId = null
        this.selectSchoolQueryParams.agentIdArray = []
        this.selectSchoolQueryParams.pageNum = 1
        this.$emit('selected-data')
      },
      //取消学校
      closeSelectSchool() {
        this.resetForm("queryForm")
        this.selectSchoolQueryParams.agentId = null
        this.selectSchoolQueryParams.agentIdArray = []
        this.selectSchoolQueryParams.pageNum = 1
        this.$emit('close-select-window')
      }
    }
  };
</script>
